<template>
  <div class="user-management">
    <el-card>
      <div class="header">
        <h2>用户管理</h2>
        <el-button type="primary" @click="handleAdd">新增用户</el-button>
      </div>
      
      <el-table :data="userList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="department" label="部门" />
        <el-table-column prop="position" label="岗位" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-select v-model="form.department" placeholder="请选择部门">
            <el-option 
              v-for="item in departments" 
              :key="item.id" 
              :label="item.name" 
              :value="item.id" 
            />
          </el-select>
        </el-form-item>
        <el-form-item label="岗位" prop="position">
          <el-select v-model="form.position" placeholder="请选择岗位">
            <el-option 
              v-for="item in positions" 
              :key="item.id" 
              :label="item.name" 
              :value="item.id" 
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const userList = ref([
  { id: 1, username: 'admin', department: '技术部', position: '管理员', password: '123456' },
  { id: 2, username: 'user1', department: '市场部', position: '市场专员', password: '123456' }
])

const departments = ref([
  { id: 1, name: '技术部' },
  { id: 2, name: '市场部' },
  { id: 3, name: '人事部' }
])

const positions = ref([
  { id: 1, name: '管理员' },
  { id: 2, name: '开发工程师' },
  { id: 3, name: '市场专员' },
  { id: 4, name: 'HR专员' }
])

const dialogVisible = ref(false)
const dialogTitle = ref('')
const formRef = ref(null)
const isEdit = ref(false)
const currentId = ref(0)

const form = reactive({
  username: '',
  password: '',
  department: '',
  position: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  department: [{ required: true, message: '请选择部门', trigger: 'change' }],
  position: [{ required: true, message: '请选择岗位', trigger: 'change' }]
}

const handleAdd = () => {
  dialogTitle.value = '新增用户'
  isEdit.value = false
  resetForm()
  dialogVisible.value = true
}

const handleEdit = (row) => {
  dialogTitle.value = '编辑用户'
  isEdit.value = true
  currentId.value = row.id
  form.username = row.username
  form.password = row.password
  form.department = departments.value.find(d => d.name === row.department)?.id
  form.position = positions.value.find(p => p.name === row.position)?.id
  dialogVisible.value = true
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该用户吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userList.value = userList.value.filter(user => user.id !== row.id)
    ElMessage.success('删除成功')
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

const submitForm = () => {
  formRef.value.validate(valid => {
    if (valid) {
      const departmentName = departments.value.find(d => d.id === form.department)?.name
      const positionName = positions.value.find(p => p.id === form.position)?.name
      
      if (isEdit.value) {
        const index = userList.value.findIndex(user => user.id === currentId.value)
        if (index !== -1) {
          userList.value[index] = {
            id: currentId.value,
            username: form.username,
            password: form.password,
            department: departmentName,
            position: positionName
          }
        }
        ElMessage.success('修改成功')
      } else {
        const newId = userList.value.length > 0 
          ? Math.max(...userList.value.map(user => user.id)) + 1 
          : 1
        
        userList.value.push({
          id: newId,
          username: form.username,
          password: form.password,
          department: departmentName,
          position: positionName
        })
        ElMessage.success('添加成功')
      }
      
      dialogVisible.value = false
    }
  })
}

const resetForm = () => {
  form.username = ''
  form.password = ''
  form.department = ''
  form.position = ''
}
</script>

<style scoped>
.user-management {
  padding: 20px;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>